package com.xunyji.demo03.realm;

import org.apache.shiro.authc.*;
import org.apache.shiro.realm.Realm;

/**
 * @author AltEnter
 * @create 2019-01-20 20:11
 * @desc 自定义简易Realm
 **/
public class MySimpleRealm implements Realm {

    public String getName() {
        return "mySimpleRealm";
    }

    public boolean supports(AuthenticationToken token) {
        if (token instanceof UsernamePasswordToken) {
            return true;
        }
        return false;
    }

    public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String username = (String) token.getPrincipal();
        String password = new String((char[])token.getCredentials());
        System.out.println(String.format("用户名为：%s， 用户密码为：%s", username, password));
        if (!"fury".equals(username)) {
            System.out.println("用户名错误");
            return null;
        }
        if (!"111111".equals(password)) {
            System.out.println("密码错误");
            return null;
        }

        return new SimpleAuthenticationInfo(username, password, getName());
    }
}

